<?php

class WallController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	Zend_Loader::loadClass('UserModel');
    	Zend_Loader::loadClass('WallModel');
    	Zend_Loader::loadClass('FriendModel');
		Zend_Loader::loadClass('FriendrequestModel');
    }
    
    function preDispatch() {
    	$auth = Zend_Auth::getInstance();
    	if (!$auth->getIdentity()->id) $this->_redirect('/');
    	else {
    		$this->viewer = $auth->getIdentity()->username;
    		$this->viewer_id = $auth->getIdentity()->id;
    		$username = $this->_request->getParam('username');
			$this->info_username = UserModel::getuserinfo($username);
			$this->info_viewer = UserModel::getuserinfo($this->viewer);
			$this->list_friend = FriendModel::listfriend($this->info_username['id']);
			$this->view->info_username = $this->info_username;
			$this->view->info_viewer = $this->info_viewer;
			$this->view->list_friend = $this->list_friend;
			$this->view->list_request = FriendrequestModel::listrequest($this->info_username['id']);
			$this->view->controller = $this->_request->getParam('controller');
			$this->view->isfriend = FriendModel::isfriend($this->info_username['id'],$this->info_viewer['id']);
			$this->view->requesting = FriendrequestModel::requesting($this->info_viewer['id'],$this->info_username['id']);
			$this->view->requested = FriendrequestModel::requesting($this->info_username['id'],$this->info_viewer['id']);
			$layout = $this->_helper->layout();
			$layout->setLayout('wall_layout');	
    	}
    }

    public function indexAction()
    {
    	$this->view->headScript()->appendFile('/js/wall.js');
    	$this->view->headLink()->appendStylesheet('/css/wall.css');
		$wall_post = WallModel::listwallpost($this->info_username['id']);
		$this->view->wall_post = $wall_post;
    }
    
    public function addAction(){
    	$wallpost['type'] = $this->_request->getParam('type');
    	$wallpost['username'] = $this->_request->getParam('username');
    	$user_info = UserModel::getuserinfo($wallpost['username']);
    	$wallpost['friend_id'] = $user_info['id'];
    	$wallpost['user_id'] = $this->viewer_id;
    	if ($wallpost['type']==1){
    		$wallpost['content'] = $this->_request->getParam('content');
    	}

    	if ($wallpost['type']==2){
    		$wallpost['photo'] = $this->_request->getParam('photo');
    	}
    	    		
    	WallModel::add($wallpost);
    	$this->_redirect('/'.$wallpost['username']);
    }
    
    public function storyAction(){

    }
    
}